<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

use app\admin\model\Customer as CustomerModel;

class Supplier extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            extract($data);
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (empty($keywork)) {
                $list = Db::name('customer')->where('is_customer', 'in', '1,3')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('customer')->where('is_customer', 'in', '1,3')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $field = 'sn|name|mark';
                $sonField = 'name|phone|telephone|address';

                $where = getSearch($field, $sonField, $keywork, ['customer_ext', 'customer_id'], true);
                $list = Db::name('customer')->where($where)->where('is_customer', 'in', '1,3')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('customer')->where($where)->where('is_customer', 'in', '1,3')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            foreach ($list as $key => &$value) {
                $value['category'] = Db::name('customer_category')->where('id', $value['category_id'])->value('name');
                // 查询首要联系人
                $username = Db::name('customer_ext')->where(['customer_id' => $value['id'], 'username' => '是'])->find();

                $value['username'] = isset($username['name']) ? $username['name'] : '';
                $value['telephone'] = isset($username['telephone']) ? $username['telephone'] : '';
                $value['phone'] = isset($username['phone']) ? $username['phone'] : '';
                $value['numbers'] = isset($username['numbers']) ? $username['numbers'] : '';
                $value['address'] = isset($username['address']) ? $username['address'] : '';
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        $category = Db::name('customer_category')->where('is_del', 0)->select();
        $this->assign('category', $category);
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('customer')->where('id', $id)->find();
        $info['end_date'] = date('Y-m-d', $info['end_date']);
        $info['category_name'] = Db::name('customer_category')->where('id', $info['category_id'])->value('name');

        $category = Db::name('customer_category')->where('is_del', 0)->select();
        $this->assign('category', $category);
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $basedata = $data['supplier_basedata'];
        $extdata = $data['supplier_extdata'];
        $model = new CustomerModel();
        // 启动事务
        Db::startTrans();
        if (empty($basedata['id'])) {
            $basedata['enterprise_id'] = $this->EnterpriseId;
            $basedata['end_date'] = strtotime($basedata['end_date']);
            $basedata['is_customer'] = 1;
            // $basedata['category_id'] = $basedata['supplier_category_id'];
            $result = $model->allowField(true)->save($basedata);
            if (!$result) {
                // 回滚事务
                Db::rollback();
                return ['status' => $status, 'msg' => $msg];
            }
            $lastid = Db::name('customer')->where(['enterprise_id' => $this->EnterpriseId, 'is_del' => 0])->getLastInsID();
            // 新增客户扩展信息
            $subInsert = [];
            foreach ($extdata as $key => $value) {
                if (!empty($value['name'])) {

                    $subInsert[$key]['customer_id'] = $lastid;
                    $subInsert[$key]['name'] = $value['name'];
                    $subInsert[$key]['telephone'] = $value['telephone'];
                    $subInsert[$key]['phone'] = $value['phone'];
                    $subInsert[$key]['numbers'] = $value['numbers'];
                    $subInsert[$key]['username'] = $value['username'];
                    $subInsert[$key]['address'] = $value['address'];
                    if (empty($value['username'])) {
                        $subInsert[$key]['username'] = 0;
                    }
                }
            }

            if (!empty($subInsert)) {
                $sub = Db::name('customer_ext')->insertAll($subInsert);
                if (!$sub) {
                    // 回滚事务
                    Db::rollback();
                    return ['status' => $status, 'msg' => $msg];
                }
            }

            // 提交事务
            Db::commit();
            $status = 1;
            $msg = "添加成功";
        } else {
            $id = $basedata['id'];
            $basedata['end_date'] = strtotime($basedata['end_date']);
            $result = $model->allowField(true)->save($basedata, ["id" => $id]);
            if ($result === false) {
                // 回滚事务
                Db::rollback();
                return ['status' => $status, 'msg' => $msg];
            }
            // 删除所有扩展信息
            $ext_info = Db::name('customer_ext')->where(['customer_id' => $id, 'is_del' => 0])->count();
            if ($ext_info > 0) {
                $del_ext = Db::name('customer_ext')->where('customer_id', $id)->update(["is_del" => 1]);
                if ($del_ext === false) {
                    // 回滚事务
                    Db::rollback();
                    return ['status' => $status, 'msg' => $msg];
                }
            }
            // 新增客户扩展信息
            $subInsert = [];
            foreach ($extdata as $key => $value) {
                if (!empty($value['name'])) {

                    $subInsert[$key]['customer_id'] = $id;
                    $subInsert[$key]['name'] = $value['name'];
                    $subInsert[$key]['telephone'] = $value['telephone'];
                    $subInsert[$key]['phone'] = $value['phone'];
                    $subInsert[$key]['numbers'] = $value['numbers'];
                    $subInsert[$key]['username'] = $value['username'];
                    $subInsert[$key]['address'] = $value['address'];
                    if (empty($value['username'])) {
                        $subInsert[$key]['username'] = '否';
                    }
                }
            }

            if (!empty($subInsert)) {
                $sub = Db::name('customer_ext')->insertAll($subInsert);
                if (!$sub) {
                    // 回滚事务
                    Db::rollback();
                    return ['status' => $status, 'msg' => $msg];
                }
            }

            // 提交事务
            Db::commit();
            $status = 1;
            $msg = "更新成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }

    // 获取用户扩展信息
    public function getExtInfo($id)
    {
        $ext_info = Db::name('customer_ext')->where(['customer_id' => $id, 'is_del' => 0])->select();
        return json($ext_info);
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $del = Db::name('customer')->where('id', $id)->update(["is_del" => 1]);
        if ($del) {
            // 删除客户扩展信息
            Db::name('customer_ext')->where('customer_id', $id)->update(["is_del" => 1]);

            $status = 1;
            $msg = "删除成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }

    // 状态
    public function status()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $model = new CustomerModel();
        $id = $data['id'];
        $result = $model->allowField(true)->save($data, ["id" => $id]);
        if ($result) {
            $status = 1;
            $msg = "更新成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }

    //添加地址
    public function address()
    {
        return $this->fetch();
    }
}
